package com.lzs.system.controller;


import com.lzs.entity.Menu;
import com.lzs.system.service.MenuService;
import com.lzs.system.vo.MenuTree;
import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/system/menu")
@ApiModel(value = "菜单管理")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 主界面左侧菜单
     * @param request
     * @return
     */
    @GetMapping("/selectMenuByUserId")
    @ApiOperation(value = "左侧菜单页面")
    public R selectMenuByUserId(HttpServletRequest request){
        // 得到当前登录人用户Id
        String token = request.getHeader("token");
        Long userId = JwtUtils.getMemberIdByJwtToken(token);
        List<MenuTree> menuTree = menuService.selectMenuByUserId(userId);
        return R.ok().data("menuTree", menuTree);
    }

    @PostMapping("/selectAllMenu")
    @ApiOperation(value = "菜单条件查询")
    public R selectAllMenu( @RequestBody Menu menu){
        R r = menuService.selectAllMenu(menu);
        return r;
    }
    @PostMapping("/selectMenuTree")
    @ApiOperation(value = "菜单树状查询")
    public R selectMenuTree(@RequestBody Menu menu){
        List<Menu> menuTrees = menuService.selectMenuTreeByRoleId(menu,null);
        return R.ok().data("menuTrees",menuTrees);
    }
    // 新增修改
    @PostMapping("/insertOrUpdateMenu")
    @ApiOperation(value = "菜单新增修改")
    public R insertOrUpdateMenu(@RequestBody Menu menu){
        return menuService.insertOrUpdateMenu(menu);
    }
    @GetMapping("/findMenuById/{menuId}")
    @ApiOperation(value = "修改回显内容")
    public R findMenuById(@PathVariable Long menuId){
        return menuService.findMenuById(menuId);
    }
    // 根据Id删除
    @DeleteMapping("/deleteMenu/{menuId}")
    @ApiOperation(value = "根据菜单Id删除")
    public R deleteMenu(@PathVariable Long menuId){
        boolean b = menuService.removeById(menuId);
        if (b){
            return R.ok().message("删除成功");
        }
        return R.ok().message("删除失败");
    }
}

